import { dirname, resolve } from 'path'
import vue from 'rollup-plugin-vue'
import { cleandir } from 'rollup-plugin-cleandir'
import { defineConfig } from 'rollup'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs';
import replace from '@rollup/plugin-replace'
import postcss from 'rollup-plugin-postcss'

// 解决esmodule 模式下 __dirname 问题
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))

export default defineConfig({
  input: './src/index.js',
  output: [
    {
      file: resolve(__dirname, 'lib/index.esm.js'),
      format: 'esm',
      exports: 'named',
      globals: {
        vue: 'Vue'
      }
    },
    {
      file: resolve(__dirname, 'lib/index.umd.js'),
      format: 'umd',
      name: 'richTextPreview',
      globals: {
        vue: 'Vue'
      }
    }
  ],
  external: [
    'Vue'
  ],
  plugins: [
    vue(),
    cleandir('lib'),
    nodeResolve(),
    commonjs({
      include: /node_modules/
    }),

    replace({
      // 解决 Uncaught ReferenceError: process is not defined
      'process.env.NODE_ENV': JSON.stringify('production'),
    }),
    postcss()
  ]
})